# utils/amap.py
import requests

class AMapAPI:
    def __init__(self, api_key):
        self.base_url = "https://restapi.amap.com/v3"
        self.api_key = '21fee2989f005e7faa3ded175e43be0c'  # 你的高德API Key

    def geocode(self, address):
        """地理编码：将地址转换为经纬度"""
        url = f"{self.base_url}/geocode/geo"
        params = {
            "key": self.api_key,
            "address": address,
            "output": "JSON"
        }
        response = requests.get(url, params=params)
        return response.json()

    def reverse_geocode(self, lng, lat):
        """逆地理编码：将经纬度转换为地址"""
        url = f"{self.base_url}/geocode/regeo"
        params = {
            "key": self.api_key,
            "location": f"{lng},{lat}",
            "output": "JSON"
        }
        response = requests.get(url, params=params)
        return response.json()

# 示例：初始化（实际使用时放到 settings.py 中）
AMAP_KEY = "21fee2989f005e7faa3ded175e43be0c"
amap_client = AMapAPI(AMAP_KEY)